package com.example.basic.web.service.zoneChannel.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basic.base.impl.BaseEntityServiceImpl;
import com.example.basic.exception.ServiceException;
import com.example.basic.utils.PinyinUtils;
import com.example.basic.utils.StringUtils;
import com.example.basic.utils.WubiUtils;
import com.example.basic.web.enums.DataCodeEnum;
import com.example.basic.web.enums.EnableTypeEnum;
import com.example.basic.web.mapper.zoneChannel.ZoneChannelMapper;
import com.example.basic.web.model.zoneChannel.ZoneChannel;
import com.example.basic.web.service.dataCode.DataCodeService;
import com.example.basic.web.service.zoneChannel.ZoneChannelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.example.basic.utils.CheckUtil.notNull;


@Service
public class ZoneChannelServiceImpl extends BaseEntityServiceImpl<ZoneChannelMapper, ZoneChannel> implements ZoneChannelService {

    @Resource
    private ZoneChannelMapper zoneChannelMapper;

    @Resource
    private DataCodeService dataCodeService;


    /**
     * 保存或者修改
     * @param zoneChannel 模型
     * @return
     */
    @Override
    public ZoneChannel addOrUpdate(ZoneChannel zoneChannel) {
        //参数检验
        notNull(zoneChannel.getZoneCode(), "分区编码不能为空");
        notNull(zoneChannel.getChannelName(),"通道名称不能为空");

        //校验名称是否重复
        LambdaQueryWrapper<ZoneChannel> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZoneChannel::getChannelName, zoneChannel.getChannelName());
        if (zoneChannel.getId() != null) {
            notNull(zoneChannel.getChannelCode(), "通道编码不能为空");
            lqw.ne(ZoneChannel::getChannelCode, zoneChannel.getChannelCode());
        }
        ZoneChannel detail = this.mapper.selectOne(lqw);
        if (detail != null) {
            throw new ServiceException("当前通道名称已存在");
        }
        //修改
        if (zoneChannel.getId() != null) {
            preUpdate(zoneChannel);
        }
        //新增
        else {
            //生成通道编码
            String channelCode = dataCodeService.generateDataCodeNo(DataCodeEnum.CHANNEL_CODE.getType());
            zoneChannel.setChannelCode(channelCode);
            preCreate(zoneChannel);
        }

        //生成拼音码五笔码
        generatePyWbCode(zoneChannel);

        this.save(zoneChannel);
        return detail(zoneChannel.getChannelCode());
    }

    /**
     * @create 生成五笔码、拼音码
     * @param zoneChannel 系统用户模型实体类
     */
    private void generatePyWbCode(ZoneChannel zoneChannel) {
        // 获取通道名
        String channelName = StringUtils.safeToString(zoneChannel.getChannelName(), "");
        // 生成拼音码
        if("".equals(StringUtils.safeToString(zoneChannel.getPyCode(),"").replaceAll(" ",""))) {
            String userNamePinyin = PinyinUtils.getSimplePinYin(channelName);
            zoneChannel.setPyCode(userNamePinyin);
        }
        // 生成拼音码
        if("".equals(StringUtils.safeToString(zoneChannel.getWbCode(),"").replaceAll(" ",""))) {
            String userNameWubi = WubiUtils.getSimpleWuBi(channelName);
            zoneChannel.setWbCode(userNameWubi);
        }
    }

    /**
     * 详情
     * @param channelCode 通道编码
     * @return
     */
    @Override
    public ZoneChannel detail(String channelCode) {
        notNull(channelCode, "通道编码不能为空");

        return zoneChannelMapper.detail(channelCode);
    }

    /**
     * 删除
     * @param channelCode 通道编码
     * @return
     */
    @Override
    public int delete(String channelCode) {
        notNull(channelCode,"通道编码不能为空");

        ZoneChannel zoneChannel = detail(channelCode);
        if(zoneChannel == null){
            throw new ServiceException("未查询到当前数据");
        }
        return this.mapper.deleteById(zoneChannel.getId());
    }

    /**
     * 启用/禁用
     * @param channelCode 通道编码
     * @return
     */
    @Override
    public int enableOrDisable(String channelCode) {
        notNull(channelCode,"通道编码不能为空");

        ZoneChannel zoneChannel = detail(channelCode);

        if(zoneChannel == null){
            throw new ServiceException("未查询到当前数据");
        }

        //停用启用
        if(zoneChannel.getEnableFlag().equals(EnableTypeEnum.ON.getType())){
            zoneChannel.setEnableFlag(EnableTypeEnum.OFF.getType());
        }else {
            zoneChannel.setEnableFlag(EnableTypeEnum.ON.getType());
        }

        preUpdate(zoneChannel);

        return this.mapper.updateById(zoneChannel);
    }

    /**
     * 通道列表
     *
     * @param queryPage   分页
     * @param searchParam 模糊搜索(拼音码,五笔码,名称,编码)
     * @param isAll       是否查询全部 0-否，1-是
     * @param pharmCode 仓库编码
     * @param zoneCode 分区编码
     * @return
     */
    @Override
    public IPage<ZoneChannel> queryList(Page<ZoneChannel> queryPage, String searchParam, String isAll, String pharmCode, String zoneCode) {
        return zoneChannelMapper.queryList(queryPage,searchParam,isAll,pharmCode,zoneCode);
    }

    /**
     * 查询分区下在用通道
     * @param zoneCode 分区编码
     * @return
     */
    @Override
    public List<ZoneChannel> queryByZone(String zoneCode) {
        notNull(zoneCode,"分区编码不能为空");

        LambdaQueryWrapper<ZoneChannel> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZoneChannel::getZoneCode,zoneCode);
        lqw.eq(ZoneChannel::getEnableFlag,EnableTypeEnum.ON.getType());

        return this.mapper.selectList(lqw);
    }

    /**
     * 删除分区下通道信息
     * @param zoneCode 分区编码
     */
    @Override
    public Boolean removeByZone(String zoneCode) {
        notNull(zoneCode,"分区编码不能为空");

        LambdaQueryWrapper<ZoneChannel> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZoneChannel::getZoneCode,zoneCode);
        this.mapper.delete(lqw);

        return true;
    }
}
